package net.asfun.jvalog.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.jdo.Query;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;

import net.asfun.jvalog.misc.Filter;
import net.asfun.jvalog.misc.Setting;
import net.asfun.jvalog.po.Message;
import net.asfun.jvalog.po.Relation;
import net.asfun.jvalog.service.Jdoer;
import net.asfun.jvalog.service.JdoerFactory;
import net.asfun.jvalog.task.UpdateManager;
import net.asfun.jvalog.view.PagerInfo;

public class Comment {
	
	private Message message;
	
	private Relation post;

	public Comment() {
		message = new Message();
		message.setDate(new Date());
		post = new Relation();
		post.setType(Bridge.POST_COMMENT);
	}
	
	private Comment(int i){};
	
	public static void remove(Long id) {
		Jdoer<Message> dbm = JdoerFactory.getMessageJdo();
		Message message = dbm.load(id);
		if ( message != null ) {
			if ( message.isPublished() ) {
				UpdateManager.enableCounterTask();
			}
			dbm.delete(Bridge.getRelationByRight(message.getKey(), Bridge.POST_COMMENT));
			dbm.delete(message);
		}
	}
	
	public Long getId() {
		return message.getKey().getId();
	}
	
	public void setUserName(String userName) {
		message.setUserName(userName);
	}
	
	public String getUserName() {
		return message.getUserName();
	}
	
	public void setWebUrl(String url) {
		//TODO member.url
	}
	
	public String getWebUrl() {
		//TODO member.url
		return "http://blog.asfun.net";
	}
	
	public void setEmail(String email) {
		message.setEmail(email);
	}
	
	public String getEmail() {
		return message.getEmail();
	}
	
	public void setContent(String content) {
		message.setContent(new Text(content));
	}
	
	public String getContent() {
		String content = message.getContent().getValue();
		content = Filter.escape(content);
		content = Filter.breakline(content);
		return content;
	}
	
	public String getPreview() {
		String content = message.getContent().getValue();
		content = Filter.length(content, 30);
		content = Filter.escape(content);
		return content;
	}
	
	public Date getDate() {
		return message.getDate();
	}
	
	public void setUser(User user) {
		message.setUserName(user.getNickname());
		message.setEmail(user.getEmail());
		//TODO member.url
	}
	
	public void setPublished(boolean isp) {
		message.setPublished(isp);
	}
	
	public boolean isPublished() {
		if (message == null) return false;
		return message.isPublished();
	}
	
	public void save() {
		Jdoer<Message> dbm = JdoerFactory.getMessageJdo();
		dbm.save(message);
		post.setRight(message.getKey());
		dbm.save(post);
	}

	public void setPostId(Long postId) {
		post.setLeft(KeyFactory.createKey("Paper", postId));
	}
	
	public Long getPostId() {
		setPost();
		return post.getLeft().getId();
	}
	
	public static List<Comment> listAll(PagerInfo pager) {
		List<Comment> list = new ArrayList<Comment>();
		Jdoer<Message> dbm = JdoerFactory.getMessageJdo();
		pager.setTotal(dbm.all().size());
		Query query = dbm.prepareQuery();
		query.setOrdering("published ascending, date descending");
		query.setRange(pager.getStart(), pager.getEnd());
		Collection<Message> res = dbm.more(query);
		for(Message msg : res) {
			Comment comm = new Comment(0);
			comm.message = msg;
			list.add(comm);
		}
		return list;
	}
	
	public Post getPost() {
		setPost();
		return Post.load(post.getLeft().getId());
	}

	private void setPost() {
		if ( post == null ) {
			post = Bridge.getRelationByRight(message.getKey(), Bridge.POST_COMMENT);
		}
	}

	public static Comment load(long id) {
		Jdoer<Message> dbm = JdoerFactory.getMessageJdo();
		Comment comm = new Comment(0);
		comm.message = dbm.load(id);
		return comm;
	}

	public void update() {
		if ( message.isPublished() ) {
			UpdateManager.enableCounterTask();
		}
	}
	
	public static List<Comment> listRecent() {
		List<Comment> list = new ArrayList<Comment>();
		Jdoer<Message> dbm = JdoerFactory.getMessageJdo();
		Query query = dbm.prepareQuery();
		query.setFilter("published == true");
		query.setOrdering("date descending");
		query.setRange(0, Setting.getRecentComments());
		Collection<Message> res = dbm.more(query);
		for(Message msg : res) {
			Comment comm = new Comment(0);
			comm.message = msg;
			list.add(comm);
		}
		return list;
	}

}
